package com.tpc.client.systeme.core.affichage;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.NamedFrame;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.FetchMode;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.TitleOrientation;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.Layout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.HTMLFlow;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.CanvasItem;
import com.smartgwt.client.widgets.form.fields.FormItemIcon;
import com.smartgwt.client.widgets.form.fields.SpacerItem;
import com.smartgwt.client.widgets.form.fields.StaticTextItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.LayoutSpacer;
import com.smartgwt.client.widgets.menu.Menu;
import com.smartgwt.client.widgets.menu.MenuItem;
import com.smartgwt.client.widgets.menu.MenuItemSeparator;
import com.smartgwt.client.widgets.menu.events.ClickHandler;
import com.smartgwt.client.widgets.menu.events.MenuItemClickEvent;
import com.smartgwt.client.widgets.tab.TabSet;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.events.TabClickEvent;
import com.smartgwt.client.widgets.tab.events.TabIconClickHandler;
import com.smartgwt.client.widgets.tab.events.TabSelectedEvent;
import com.smartgwt.client.widgets.tab.events.TabSelectedHandler;
import com.smartgwt.client.widgets.toolbar.ToolStrip;
import com.smartgwt.client.widgets.toolbar.ToolStripButton;
import com.smartgwt.client.widgets.toolbar.ToolStripMenuButton;
import com.tpc.client.modules.core.contact.ContactUI;
import com.tpc.client.modules.core.organisation.OrganisationUI;
import com.tpc.client.modules.core.dashboard.DashboardUi;
import com.tpc.client.systeme.core.Systeme;
import com.tpc.client.systeme.core.TPCT_ADMIN_MODULE;
import com.tpc.client.systeme.core.TPCT_MODULE;
import com.tpc.client.systeme.core.datasource.LoginDataSource;
import com.tpc.client.systeme.core.i18n.Dictionary;
import java.util.Date;

/*
 * Here you build the main page. You should set :
 * Conent Window Width, background color, form to edit, list, etc.
 *
 * ****************** IMPORTANT *************
 * MODULES & APPLICATIONS & TABS & MENUS
 * Modules, applications, tabs and menus will change from
 * one system setup to another, please review carefully !
 *
 */

public class UImanager implements EntryPoint {
    Dictionary dictionary = (Dictionary) GWT.create(Dictionary.class);
    Systeme systeme = new Systeme();
    Canvas CANVAS;
    final private VLayout vltMainPage = new VLayout();

    //SET UI PARAMETERS : COLOR, WIDTH, HEIGHT, PADDING //
    Img imgWorkingGif = new Img("loader.gif",128,15);
    String CanvasWidth = "100%";
    String CanvasHeight = "100%";
    String CanvasBGcolor = "#3A6EA5";

    int WindowWidth = 0;
    int WindowHeight = 0;
    
    /** HERE IS THE REAL WIDTH FOR THE WINDOW OF YOUR APPLICATION : SET WIDTH AT 0 TO GET A FULL SCREEN WINDOW WITH A 2px MARGIN ALL AROUND MARGIN COLOR = CANVASBGCOLOR **/
    String MainPageWidth = "990";
    String MainPageHeight = "100%";
    String MainPageBGcolor = "#E7E7E7";
    String MainPageRightPadding = "*";
    String MainPageLeftPadding = "*";

    String topSpacerWidth = "100%";
    String topSpacerHeight = "1";
	String topSpacerBGcolor = "#3A6EA5";
    /** middle spacer is space between menubar and appbar **/
    String moduleSpacerWidth = "100%";
    String moduleSpacerHeight = "1";
    String moduleSpacerBGcolor= "#E7E7E7";
    String appSpacerWidth = "100%";
    String appSpacerHeight = "5";
    String appSpacerBGcolor= "#E7E7E7";
    String bottomSpacerWidth = "100%";
    String bottomSpacerHeight = "1";
    String bottomSpacerBGcolor= "#3A6EA5";
    
    String SideBarBGcolor = "#3A6EA5";

    String menuBarHeight = "25";
    String menuBarWidth = "100%";
    String menuBarBGcolor = "#E7E7E7";
    int menuBarpadding = 2;

    String moduleBarHeight = "1";
    String moduleBarWidth = "100%";
    String moduleBarBGcolor = "#E7E7E7";
    int moduleBarpadding = 0;

    String statusBarWidth = "100%";
    String statusBarHeight = "25";
    String statusBarBGcolor= "#E7E7E7";
    int statusBarPadding = 2;

    String toolStripWidth = "100%";
    String toolStripHeight = "25";
    String toolStripBGcolor = "#E7E7E7";

    Boolean menuShowShadow = true;
    int menuShadowDepth = 10;

    String TabPaneWidth = "100%";
    String TabPaneHeight = "100%";
    int TabPaneMargin = 0;
    String TabPaneBGcolor = "#FFFFFF";
    Overflow TabPaneOverFlow = Overflow.HIDDEN;

    /** All list through the system **/
    String GridWidth = "100%";
    String GridHeight = "100%";
    String GridBGcolor = "#FFFFFF";
    Boolean GridAlternateRecord = Boolean.TRUE;
    Boolean GridShowEmptyMsg = Boolean.TRUE;
    Boolean GridShowEditor = Boolean.TRUE;
    Boolean GridAutoFetchData = Boolean.FALSE;
    FetchMode GridDataFetchMode = FetchMode.LOCAL;

    /** All form through the system **/
    String FormWidth = "100%";
    String FormHeight = "100%";
    String FormBGcolor = "#F5F5F5";
    int FormCellpadding = 3;
    Alignment FormAlign = Alignment.CENTER;
    TitleOrientation FormTitleOrientation = TitleOrientation.TOP;
    String FormTitleSuffixe = "";
    Boolean FormHiliteRequired = Boolean.TRUE;
    String FormRTitlePrefix = "";
    String FormRTitleSuffix = "*";
    Boolean FormAutoFetch = Boolean.FALSE;

    String HomePanelWidth = "100%";
    String HomePanelHeight = "200";
    int HomePanelShadowDepth = 4;
    Boolean HomePanelShowShadow = true;
    String HomePanelBGcolor = "#FFFFFF";

    Boolean isHelpWindowBuilded = false;
    com.smartgwt.client.widgets.Window helpWindow = new com.smartgwt.client.widgets.Window();
    String window_about_bgcolor = "#FFFFFF";
    String window_about_form_bgcolor = "#FFFFFF";
    String window_about_form_bottom_bgcolor = "#D4D0C8";

    ToolStripButton chatButton;
    Boolean isChatButtonBuilded = false;
    
    /* TEXT/LOCALE : NAMES, TITLES, MENU, ETC. ************************************************************** ************************************************************** ************************* */
    
    /** MENU TO DISPLAY ***********************************/
    Boolean showMenuSystem = true;
    Boolean showMenuDisplay = true;
    Boolean showMenuNew = false;
    Boolean showMenuAdmin = false;
    Boolean showMenuSearch = true;
    Boolean showMenuLogout = true;
    Boolean showMenuChatButton = true;
    
    String[] MENU_ADMIN_ITEMS = null;//{dictionary.REPORT};
    Object[] MENU_ADMIN_CLASSES = null;//{};
    
    /** ABOUT WINDOW **/
    String WindowTitle  = com.google.gwt.user.client.Window.getTitle();
    String APP_VERSION = dictionary.SYSTEME_VERSION();
    String APP_NAME = dictionary.SYSTEME_TITLE();
    String APP_CONTACT_INFO = "Info.: info@tpctechnologies.net - 1.514.312.4574";
    String APP_COPYRIGHT = "<B>TPCT</B> : <I>TPC Technologies Inc.</I>.<br><br>Cette application a été développée par TPC Technologies Inc. Toutes modifications, reproduction, ventes doit être approuvées par TPC Technologies Inc. 2010-2012";
    
    /** CHAT WINDOW **/
    String CHAT_URL_OPEN = "http://pme123.com/chat_window.jsp";
    String CHAT_URL_CLOSED = "http://pme123.com/send_email.jsp";
    String CHAT_WINDOW_NAME = "tpct_syteme_chat";
    String CHAT_WINDOW_WIDTH = "414";
    String CHAT_WINDOW_HEIGHT = "514";
    
    /** MODULE USED IN THE APP *********************************/
    String[] MODULES_ID = {dictionary.MODULE_HOME_ID(),dictionary.MODULE_ORGANISATION_ID(),"MAIL","TICKET","ORDER","INVENTORY","IDENTITY","KB"};
    String[] MODULES_TITLE = {dictionary.MODULE_HOME_TITLE(),dictionary.MODULE_ORGANISATION_TITLE(),"MSG","TICKETS","ORDERS","Inventaire","Identité","KB"};
    Boolean[] MODULES_DISABLED = {false,false,true,true,true,true,true,true};
    Boolean[] MODULES_CANADD = {false,true,true,true,true,true,true,true};
    int[] MODULES_SPLASH = {0,0,0,0,0,0,0};
    
    DashboardUi dashboardUI = new DashboardUi();
    OrganisationUI orgUI = new OrganisationUI();
    ContactUI contactUI = new ContactUI();
    
    Object[] MODULES_CLASSES = {dashboardUI,orgUI,null,null,null,null,null,null};

    NamedFrame uploadFrame;
    
    public void onModuleLoad() {        
        systeme.setDashboardUI(dashboardUI);
        systeme.setOrganisationUI(orgUI);
        systeme.setContactUI(contactUI);
//** END - FINISH - TERMINATE - EDITING ******************************* END - FINISH - TERMINATE - EDITING **************************** END - FINISH - TERMINATE - EDITING ************************************** END - FINISH - TERMINATE - EDITING ************************************************************************************************* //
        
        systeme.setNbrModule(MODULES_ID.length);
        systeme.setnbrModuleToHideSplash(MODULES_SPLASH);
        
        systeme.setDictionary(dictionary);
        
        systeme.setLoading(imgWorkingGif);
        
        systeme.setMainPageWidth(MainPageWidth);

        systeme.setTabPaneWidth(TabPaneWidth);
        systeme.setTabPaneHeight(TabPaneHeight);
        systeme.setTabPaneMargin(TabPaneMargin);
        systeme.setTabPaneBGcolor(TabPaneBGcolor);
        systeme.setTabPaneOverflow(TabPaneOverFlow);

        systeme.setGridWidth(GridWidth);
        systeme.setGridHeight(GridHeight);
        systeme.setGridBGcolor(GridBGcolor);
        systeme.setGridAlternate(GridAlternateRecord);
        systeme.setGridEmptyMessage(dictionary.GRID_EMPTY_MSG());
        systeme.setGridShowEmptyMsg(GridShowEmptyMsg);
        systeme.setGridLoadingMsg(dictionary.GRID_LOADING_MSG());
        systeme.setGridShowFilterEditor(GridShowEditor);
        systeme.setGridAutoFetchData(GridAutoFetchData);
        systeme.setGridDataFetchMode(GridDataFetchMode);

        systeme.setFormWidth(FormWidth);
        systeme.setFormHeight(FormHeight);
        systeme.setFormBGcolor(FormBGcolor);
        systeme.setFormCellPadding(FormCellpadding);
        systeme.setFormAlign(FormAlign);
        systeme.setFormTitleOrientation(FormTitleOrientation);
        systeme.setFormTitleSuffixe(FormTitleSuffixe);
        systeme.setFormHiliteRequired(FormHiliteRequired);
        systeme.setFormRtitlePrefix(FormRTitlePrefix);
        systeme.setFormRTitleSuffix(FormRTitleSuffix);
        systeme.setFormAutoFetchData(FormAutoFetch);

        systeme.setHomePanelWidth(HomePanelWidth);
        systeme.setHomePanelHeight(HomePanelHeight);
        systeme.setHomePanelShadowDepth(HomePanelShadowDepth);
        systeme.setHomePanelShowShadow(HomePanelShowShadow);
        systeme.setHomePanelBGcolor(HomePanelBGcolor);

        WindowWidth = com.google.gwt.user.client.Window.getClientWidth();
        WindowHeight = com.google.gwt.user.client.Window.getClientHeight();
        CANVAS = getMainCanvas();
        CANVAS.addChild(getMainPage());
        CANVAS.draw();
    }

    public Canvas getMainCanvas() {
        Canvas cnvMainCanvas = new Canvas("MAIN_CANVAS");
        cnvMainCanvas.setWidth(CanvasWidth);
        cnvMainCanvas.setHeight(CanvasHeight);
        cnvMainCanvas.setBackgroundColor(CanvasBGcolor);
        cnvMainCanvas.setOverflow(Overflow.HIDDEN);
        cnvMainCanvas.setAlign(Alignment.CENTER);
        return cnvMainCanvas;
    }

    public HLayout getMainPage() {
        HLayout hltPage = new HLayout();
        hltPage.setWidth100();
        hltPage.setHeight100();
        hltPage.setMargin(5);

            vltMainPage.setID("MAIN_PAGE");
            vltMainPage.setWidth(getMainPageWidth());
            vltMainPage.setHeight(MainPageHeight);
            vltMainPage.setBackgroundColor(MainPageBGcolor);
            vltMainPage.setShadowDepth(5);
            vltMainPage.setShowShadow(Boolean.TRUE);

            LayoutSpacer lytPaddingLeft = new LayoutSpacer();
            lytPaddingLeft.setWidth(MainPageLeftPadding);
            systeme.setLeftPadding(lytPaddingLeft);

            LayoutSpacer lytPaddingRight = new LayoutSpacer();
            lytPaddingRight.setWidth(MainPageRightPadding);

        hltPage.addMember(lytPaddingLeft);
        hltPage.addMember(getSideBarLeft());
        hltPage.addMember(getMainContent());
        hltPage.addMember(getSideBarRight());
        hltPage.addMember(lytPaddingRight);

        return hltPage;
    }

    public Layout getMainContent() {
        // TOP Spacer : Space between top of the browser page and the start of the application window
        HLayout lytTopSpacer = new HLayout();
        lytTopSpacer.setWidth(topSpacerWidth);
        lytTopSpacer.setHeight(topSpacerHeight);
        lytTopSpacer.setBackgroundColor(topSpacerBGcolor);

        // SPACER : between menuBar and ModuleBar
        LayoutSpacer lytSpacerModule = new LayoutSpacer();
        lytSpacerModule.setWidth(moduleSpacerWidth);
        lytSpacerModule.setHeight(moduleSpacerHeight);
        lytSpacerModule.setBackgroundColor(moduleSpacerBGcolor);

        // SPACER : between moduleBar and AppBar
        LayoutSpacer lytSpacerApp = new LayoutSpacer();
        lytSpacerApp.setWidth(appSpacerWidth);
        lytSpacerApp.setHeight(appSpacerHeight);
        lytSpacerApp.setBackgroundColor(appSpacerBGcolor);

        // BOTTOM Spacer : Space between bottom of the browser page and the end of the application window
        HLayout lytBottomSpacer = new HLayout();
        lytBottomSpacer.setWidth(bottomSpacerWidth);
        lytBottomSpacer.setHeight(bottomSpacerHeight);
        lytBottomSpacer.setBackgroundColor(bottomSpacerBGcolor);

        vltMainPage.addMember(lytTopSpacer);
        vltMainPage.addMember(getMenuBar());
        vltMainPage.addMember(lytSpacerModule);
        /** Module are usually in a menu "MODULES" but sometime client wants a modulebar**/
        vltMainPage.addMember(getModuleBar());
        vltMainPage.addMember(lytSpacerApp);
        vltMainPage.addMember(getAppBar());
        vltMainPage.addMember(getStatusBar());
        vltMainPage.addMember(lytBottomSpacer);

        systeme.setMainPage(vltMainPage);

        return vltMainPage;
    }

    /* Menu Bar : menu (!), loading gif when we're working and serach box */
    private HLayout getMenuBar() {
        HLayout hltMenuBar = new HLayout();
        hltMenuBar.setHeight(menuBarHeight);
        hltMenuBar.setWidth(menuBarWidth);
        hltMenuBar.setPadding(menuBarpadding);

            ToolStrip toolStrip = new ToolStrip();
            toolStrip.setWidth(toolStripWidth);
            toolStrip.setBackgroundColor(toolStripBGcolor);

                /** SET MENU IN TOOLBAR **/
                if (showMenuSystem) {
                    toolStrip.addMenuButton(getMenuSysteme());
                    toolStrip.addSeparator();
                }
                if (showMenuDisplay) {
                    toolStrip.addMenuButton(getMenuDisplay());
                    toolStrip.addSeparator();
                }
                if (showMenuNew) {
                    toolStrip.addMenuButton(getMenuNew());
                    toolStrip.addSeparator();
                }
                if (showMenuAdmin) {
                    toolStrip.addMenuButton(getMenuAdmin());
                    toolStrip.addSeparator();
                }
                if (showMenuChatButton) {
                    toolStrip.addButton(getChatButton());
                    toolStrip.addSeparator();
                }
                if (showMenuLogout) {
                    toolStrip.addButton(getLogoutButton());
                    toolStrip.addFill();
                }
                    
                toolStrip.addMember(getCanvasLoading());

                toolStrip.addFill();
                
                if (showMenuSearch) {
                    toolStrip.addFormItem(getSearchBox());
                }
                
        hltMenuBar.addMember(toolStrip);
        return hltMenuBar;
    }

    private HLayout getModuleBar() {
        HLayout hltModuleBar = new HLayout();
        hltModuleBar.setHeight(moduleBarHeight);
        hltModuleBar.setWidth(moduleBarWidth);
        hltModuleBar.setBackgroundColor(moduleBarBGcolor);

        return hltModuleBar;
    }

    /*APP_BAR will hold the Applications Tab (each one holding the maincontent) and probably an ACTION_BAR */
    private TabSet getAppBar() {
        final TabSet AppBar = new TabSet();
        AppBar.setID("APPLICATIONBAR_TABSET");
        AppBar.setWidth("100%");
        AppBar.setHeight("*");
        AppBar.setShowEdges(Boolean.FALSE);
        AppBar.setOverflow(Overflow.HIDDEN);
        AppBar.setPaneMargin(0);
        
        if (MODULES_ID != null) {
            for (int i = 0;i < MODULES_ID.length; i++) {
                if (MODULES_TITLE[i] != null) {
                    Tab newTab = new Tab(MODULES_TITLE[i]);
                    int width = 85;
                    if (MODULES_TITLE[i].length() > 8) {
                        width = 125;
                    }
                    newTab.setWidth(width);
                    newTab.setID(MODULES_ID[i]);
                    if (MODULES_CLASSES[i] != null) {
                        newTab.setPane(((TPCT_MODULE)MODULES_CLASSES[i]).loadUI(systeme,MODULES_ID[i]));
                    }
                    newTab.setDisabled(MODULES_DISABLED[i]);
                    if (MODULES_CANADD[i]) {
                        newTab.setIcon("TabAdd.png");
                    }
                    else if (i == 0) {
                    	newTab.setIcon("refresh.png");
                    }
                    AppBar.addTab(newTab);
                }
            }
        }
        
        AppBar.addTabIconClickHandler(new TabIconClickHandler() {
            public void onTabIconClick(TabClickEvent event) {
                systeme.startLoading();
                com.google.gwt.user.client.Window.setTitle(WindowTitle+" - "+event.getTab().getTitle());
                String TAB_ID = event.getTab().getID();
                if (MODULES_ID != null) {
                    for (int i = 0; i < MODULES_ID.length; i++) {
                        if (MODULES_CLASSES[i] != null) {
                            if (TAB_ID.equalsIgnoreCase(MODULES_ID[i])) {
                                ((TPCT_MODULE)MODULES_CLASSES[i]).loadUI(systeme,MODULES_ID[i]);
                                if (((TPCT_MODULE)MODULES_CLASSES[i]).getForm() != null) {
                                    ((TPCT_MODULE)MODULES_CLASSES[i]).getForm().setOverflow(systeme.getFormOverflow());
                                }                                
                                AppBar.updateTab(event.getTab(),((TPCT_MODULE)MODULES_CLASSES[i]).showAndCreate(null));
                            }
                        }
                    }
                }
            }
        });

        AppBar.addTabSelectedHandler(new TabSelectedHandler() {
            public void onTabSelected(TabSelectedEvent event) {
                com.google.gwt.user.client.Window.setTitle(WindowTitle+" - "+event.getTab().getTitle());
                if (MODULES_ID != null) {
                    for (int i = 0; i < MODULES_ID.length; i++) {
                        if (MODULES_CLASSES[i] != null) {
                            if (event.getTab().getID().equalsIgnoreCase(MODULES_ID[i])) {
                                systeme.startLoading();                               
                                ((TPCT_MODULE)MODULES_CLASSES[i]).loadUI(systeme,MODULES_ID[i]);
                                if (((TPCT_MODULE)MODULES_CLASSES[i]).getForm() != null) {
                                    ((TPCT_MODULE)MODULES_CLASSES[i]).getForm().setOverflow(systeme.getFormOverflow());
                                }
                                ((TPCT_MODULE)MODULES_CLASSES[i]).loadList();
                            }
                        }
                    }
                }
            }
        });
        Tab TAB_EXTRA = new Tab();
        TAB_EXTRA.setID(dictionary.TAB_EXTRA());
        AppBar.addTab(TAB_EXTRA);
        AppBar.selectTab(MODULES_ID[0]);

        systeme.setAppBar(AppBar);
        return AppBar;
    }

    // STATUS BAR : holds system messages, popup, icons indicator, etc.
    private HLayout getStatusBar() {
        HLayout hltStatusBar = new HLayout();
        hltStatusBar.setWidth(statusBarWidth);
        hltStatusBar.setHeight(statusBarHeight);
        hltStatusBar.setPadding(statusBarPadding);
        hltStatusBar.setBackgroundColor(statusBarBGcolor);
        
            HLayout hltNotificationBar = new HLayout();
            hltNotificationBar.setStyleName("style_Bar_notification");
            hltNotificationBar.setWidth("100%");
            hltNotificationBar.setHeight("100%");

        hltStatusBar.addMember(hltNotificationBar);

        return hltStatusBar;
    }

    private VLayout getSideBarLeft() {
        VLayout sideBar = new VLayout();
        sideBar.setWidth("1");
        int SideBarHeight = WindowHeight;
        SideBarHeight = SideBarHeight-new Integer(topSpacerHeight).intValue();
        SideBarHeight = SideBarHeight-new Integer(bottomSpacerHeight).intValue();
        sideBar.setHeight(SideBarHeight);
        sideBar.setShowResizeBar(Boolean.FALSE);
        sideBar.setCanDragResize(Boolean.FALSE);
        sideBar.setAlign(VerticalAlignment.TOP);
        sideBar.setBackgroundColor(SideBarBGcolor);
        return sideBar;
    }

    private VLayout getSideBarRight() {
        VLayout sideBar = new VLayout();
        sideBar.setWidth("1");
        int SideBarHeight = WindowHeight;
        SideBarHeight = SideBarHeight-new Integer(topSpacerHeight).intValue();
        SideBarHeight = SideBarHeight-new Integer(bottomSpacerHeight).intValue();
        sideBar.setHeight(SideBarHeight);
        sideBar.setShowResizeBar(Boolean.FALSE);
        sideBar.setCanDragResize(Boolean.FALSE);
        sideBar.setAlign(VerticalAlignment.TOP);
        sideBar.setBackgroundColor(SideBarBGcolor);
        return sideBar;
    }

    private String getMainPageWidth() {
        if (MainPageWidth != null) {
            if (MainPageWidth.equals("1")) {
                MainPageLeftPadding = "1";
                MainPageRightPadding = "1";
                return new Integer(WindowWidth).toString();
            }
            else {
                return MainPageWidth;
            }
        }
        else {
            return "100%";
        }
    }

    private ToolStripMenuButton getMenuSysteme() {
        /** SYSTEME **/
        Menu menuSystem = new Menu();
        menuSystem.setShowShadow(menuShowShadow);
        menuSystem.setShadowDepth(menuShadowDepth);

            MenuItem menuItemHome = new MenuItem(dictionary.MENU_SYSTEME_HOME());
            menuItemHome.addClickHandler(new ClickHandler() {
                public void onClick(MenuItemClickEvent event) {
                    systeme.getAppBar().selectTab(MODULES_ID[0]);
                }
            });
            MenuItemSeparator separator = new MenuItemSeparator();
            MenuItem menuItemOption = new MenuItem(dictionary.SEARCH());
            menuItemOption.setEnabled(Boolean.FALSE);
            MenuItem menuItemOwner = new MenuItem(dictionary.MENU_SYSTEME_PREFERENCES());
            menuItemOwner.setEnabled(Boolean.FALSE);
            MenuItem menuItemRessources = new MenuItem(dictionary.MENU_SYSTEME_RESSOURCE());
            menuItemRessources.setEnabled(Boolean.FALSE);
            MenuItem menuItemExport = new MenuItem(dictionary.MENU_SYSTEME_EXPORT());
            menuItemExport.setEnabled(Boolean.FALSE);
            MenuItem menuItemHelp = new MenuItem(dictionary.MENU_SYSTEME_ABOUT());
            menuItemHelp.setEnabled(Boolean.TRUE);
            menuItemHelp.addClickHandler(new ClickHandler() {
                @Override
                public void onClick(MenuItemClickEvent event) {
                    getHelpWindow().show();
                }
            });
            MenuItem menuItemPrint = new MenuItem(dictionary.PRINT());
            menuItemPrint.setEnabled(Boolean.FALSE);
            menuItemPrint.addClickHandler(new ClickHandler() {
                public void onClick(MenuItemClickEvent event) {
                    Window.print();
                }
            });
            MenuItem menuItemExit = new MenuItem(dictionary.MENU_SYSTEME_EXIT());
            menuItemExit.addClickHandler(new ClickHandler() {
                public void onClick(MenuItemClickEvent event) {
                    logout();
                }
            });

        menuSystem.setItems(menuItemHome,separator,menuItemOption,menuItemOwner,menuItemRessources,menuItemHelp,menuItemExport,menuItemPrint,separator,menuItemExit);

        ToolStripMenuButton menuButtonSysteme = new ToolStripMenuButton(dictionary.MENU_SYSTEME(),menuSystem);
        return menuButtonSysteme;
    }

    private ToolStripMenuButton getMenuDisplay() {
        /** DISPLAY **/
        Menu menuSystem = new Menu();
        menuSystem.setShowShadow(menuShowShadow);
        menuSystem.setShadowDepth(menuShadowDepth);

            MenuItem menuItemPrintLayout = new MenuItem(dictionary.MENU_DISPLAY_PRINT());
            menuItemPrintLayout.addClickHandler(new ClickHandler() {
                public void onClick(MenuItemClickEvent event) {
                    systeme.getLeftPadding().setWidth(0);
                    systeme.getMainPage().setWidth(systeme.getMainPageWidth());
                    systeme.getRightPadding().setWidth(0);
                }
            });
            MenuItem menuItemCenterLayout = new MenuItem(dictionary.MENU_DISPLAY_CENTER());
            menuItemCenterLayout.addClickHandler(new ClickHandler() {
                public void onClick(MenuItemClickEvent event) {
                    systeme.getLeftPadding().setWidth("*");
                    systeme.getMainPage().setWidth(systeme.getMainPageWidth());
                    systeme.getRightPadding().setWidth("*");
                }
            });
            MenuItem menuItemFullLayout = new MenuItem(dictionary.MENU_DISPLAY_FULLSCREEN());
            menuItemFullLayout.addClickHandler(new ClickHandler() {
                public void onClick(MenuItemClickEvent event) {
                    systeme.getLeftPadding().setWidth(1);
                    systeme.getMainPage().setWidth("100%");
                    systeme.getRightPadding().setWidth(1);
                }
            });
                       
            final MenuItem menuItemScrollbar = new MenuItem(dictionary.MENU_DISPLAY_SCROLLBAR());
            menuItemScrollbar.addClickHandler(new ClickHandler() {
                public void onClick(MenuItemClickEvent event) {
                    if (systeme.getFormOverflow() == Overflow.HIDDEN) {
                        systeme.setFormOverflow(Overflow.VISIBLE);
                        menuItemScrollbar.setChecked(Boolean.TRUE);
                    }
                    else {
                        systeme.setFormOverflow(Overflow.HIDDEN);
                        menuItemScrollbar.setChecked(Boolean.FALSE);
                    }
                    String currentTAB = systeme.getAppBar().getSelectedTab().getID();
                    if (MODULES_ID != null) {
                        for (int i = 0; i < MODULES_ID.length; i++) {
                            if (MODULES_ID[i].equalsIgnoreCase(currentTAB)) {
                                if (MODULES_CLASSES[i] != null) {
                                    ((TPCT_MODULE)MODULES_CLASSES[i]).loadUI(systeme,MODULES_ID[i]);
                                    ((TPCT_MODULE)MODULES_CLASSES[i]).getForm().setOverflow(systeme.getFormOverflow());
                                }
                            }
                        }
                    }
                }
            });


        menuSystem.setItems(menuItemPrintLayout,menuItemCenterLayout,menuItemFullLayout,menuItemScrollbar);

        ToolStripMenuButton menuButtonSysteme = new ToolStripMenuButton(dictionary.MENU_DISPLAY(),menuSystem);
        return menuButtonSysteme;
    }
    
    private Canvas getCanvasLoading() {
        Canvas canvasLoading = new Canvas("CANVAS_LOADER");
        canvasLoading.setWidth(128);
        canvasLoading.setHeight(15);
        canvasLoading.setBackgroundColor(toolStripBGcolor);
        canvasLoading.setAlign(Alignment.CENTER);

            systeme.getLoading();
            systeme.stopLoading();

        canvasLoading.addChild(imgWorkingGif);
        
        return canvasLoading;
    }
    
    private TextItem getSearchBox() {
        TextItem searchBox = new TextItem("FORM_HOME_SEARCHBOX");
        searchBox.setWidth(190);
        searchBox.setHeight(22);
        searchBox.setDisabled(Boolean.TRUE);
        searchBox.setTextBoxStyle("style_BarMenu_SearchBox");
        searchBox.setShowTitle(Boolean.FALSE);

            FormItemIcon searchLens = new FormItemIcon();
            searchLens.setSrc("searchLens.png");

        searchBox.setIcons(searchLens);
        
        return searchBox;
    }
    
    private ToolStripButton getLogoutButton() {
        ToolStripButton logoutButton = new ToolStripButton(dictionary.LOGOUT());
        logoutButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                logout();
            }
        });
        return logoutButton;
    }
    
    private ToolStripMenuButton getMenuAdmin() {
        Menu menu = new Menu();
        menu.setShowShadow(menuShowShadow);
        menu.setShadowDepth(menuShadowDepth);

        if (MENU_ADMIN_ITEMS != null) {
            for (int i = 0; i < MENU_ADMIN_ITEMS.length; i++) {
                final MenuItem menuAdminItem = new MenuItem(MENU_ADMIN_ITEMS[i]);
                final TPCT_ADMIN_MODULE CURRENT_CLASSE = (TPCT_ADMIN_MODULE)MENU_ADMIN_CLASSES[i];
                menuAdminItem.addClickHandler(new ClickHandler() {
                    public void onClick(MenuItemClickEvent event) {
                        if (systeme.isAdmin()) {
                            systeme.getAppBar().updateTab(systeme.getAppBar().getTab(dictionary.TAB_EXTRA()),CURRENT_CLASSE.loadUI(systeme,null));
                        }
                        else {
                            SC.say(dictionary.ACCESS_DENIED_ADMIN_ONLY());
                        }
                    }
                });

                menu.addItem(menuAdminItem,i);
            }
        }
        ToolStripMenuButton menuButtonSysteme = new ToolStripMenuButton(dictionary.MENU_ADMIN(),menu);
        return menuButtonSysteme;
    }
    
    private ToolStripMenuButton getMenuNew() {
         /** NEW/ADD **/
        Menu menu = new Menu();
        menu.setShowShadow(menuShowShadow);
        menu.setShadowDepth(menuShadowDepth);

            if (MODULES_ID != null) {
                for (int i = 0; i < MODULES_ID.length; i++) {
                    if (MODULES_CANADD[i]) {
                        final int x = i;
                        MenuItem newMenuItem = new MenuItem(MODULES_TITLE[i]);
                        Boolean menuEnabled = true;
                        if (MODULES_DISABLED[i]) {
                            menuEnabled = false;
                        }
                        newMenuItem.setEnabled(menuEnabled);
                        newMenuItem.addClickHandler(new ClickHandler() {
                            @Override
                            public void onClick(MenuItemClickEvent event) {
                                systeme.getAppBar().updateTab(systeme.getAppBar().getTab(MODULES_ID[x]),((TPCT_MODULE)MODULES_CLASSES[x]).showAndCreate(null));
                            }
                        });
                        menu.addItem(newMenuItem);
                    }
                }
            }

        ToolStripMenuButton menuButton = new ToolStripMenuButton(dictionary.MENU_NEW()+"...",menu);
        return menuButton;
    }
    
    public ToolStripButton getChatButton() {
        if (!isChatButtonBuilded) {
            chatButton = new ToolStripButton(dictionary.BUTTON_CHAT());
            chatButton.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
                @Override
                public void onClick(ClickEvent event) {
                    String chatUrl = CHAT_URL_OPEN;
                    Date today = new Date();
                    if (((today.getHours() < 9) || (today.getHours() > 16)) || ((today.getDay() == 0) || (today.getDay() == 6))) {
                        chatUrl = CHAT_URL_CLOSED;
                    }
                    Window.open(chatUrl,CHAT_WINDOW_NAME,"width="+CHAT_WINDOW_WIDTH+",height="+CHAT_WINDOW_HEIGHT);
                }
            });
            isChatButtonBuilded = true;
        }
        return chatButton;        
    }
    
    private void buildHelpWindow() {
        if (!isHelpWindowBuilded) {
            helpWindow.setID("WINDOW_SYSTEM_HELP");
            helpWindow.setWidth(500);
            helpWindow.setHeight(309);
            helpWindow.centerInPage();
            helpWindow.setIsModal(Boolean.TRUE);
            helpWindow.setCanDragResize(Boolean.FALSE);
            helpWindow.setShowMinimizeButton(Boolean.FALSE);
            helpWindow.setShowMaximizeButton(Boolean.FALSE);
            helpWindow.setTitle(dictionary.MENU_SYSTEME_ABOUT());
            
                VLayout winLayout = new VLayout();
                winLayout.setWidth100();
                winLayout.setHeight100();
                
                    VLayout top = new VLayout();
                    top.setWidth100();
                    top.setHeight("144");
                    top.setBackgroundColor(window_about_bgcolor);
                        
                        DynamicForm formDisplay = new DynamicForm();
                        formDisplay.setBackgroundColor(window_about_form_bgcolor);
                        formDisplay.setWidth100();
                        formDisplay.setHeight100();
                        formDisplay.setNumCols(3);
                            
                            SpacerItem spacerTopRow = new SpacerItem();
                            spacerTopRow.setColSpan(3);
                            spacerTopRow.setWidth("100%");
                            spacerTopRow.setHeight(22);
                        
                            SpacerItem spacer = new SpacerItem();
                            spacer.setWidth(15);
                            spacer.setRowSpan(3);
                        
                            StaticTextItem AppTitle = new StaticTextItem();
                            AppTitle.setShowTitle(Boolean.FALSE);
                            AppTitle.setValue(APP_NAME);
                            AppTitle.setTextBoxStyle("style_Text_Static_Big");
                            AppTitle.setHeight(40);
                            AppTitle.setWidth(290);
                            
                            CanvasItem cnvLogo = new CanvasItem();
                            cnvLogo.setShowTitle(Boolean.FALSE);
                            cnvLogo.setRowSpan(3);
                                Img Logo = new Img("LogoAbout.png");
                                Logo.setWidth(122);
                                Logo.setHeight(122);
                            cnvLogo.setCanvas(Logo);
                            
                            StaticTextItem AppVersion = new StaticTextItem();
                            AppVersion.setShowTitle(Boolean.FALSE);
                            AppVersion.setValue(APP_VERSION);
                            AppVersion.setTextBoxStyle("style_Text_Static_Small");
                            AppVersion.setHeight(20);
                            AppTitle.setWidth(260);
                            
                            SpacerItem spacerBottomMiddle = new SpacerItem();
                            spacerBottomMiddle.setHeight("*");
                                                            
                        formDisplay.setItems(spacerTopRow,spacer,AppTitle,cnvLogo,AppVersion,spacerBottomMiddle);
                        
                    top.addMember(formDisplay);
                    
                    VLayout bottom = new VLayout();
                    bottom.setWidth100();
                    bottom.setHeight("*");
                    bottom.setBackgroundColor(window_about_form_bottom_bgcolor);
                    
                        
                        Label AppCopyright = new Label(APP_COPYRIGHT);
                        AppCopyright.setStyleName("style_Text_Static_Small");
                        AppCopyright.setMargin(5);
                        AppCopyright.setWidth100();
                        AppCopyright.setHeight(75);
                        
                        DynamicForm formCloseAbout = new DynamicForm();
                        formCloseAbout.setNumCols(3);
                        formCloseAbout.setWidth100();
                        formCloseAbout.setHeight(30);
                        
                            CanvasItem cnvCheckUpdate = new CanvasItem();
                            cnvCheckUpdate.setWidth(60);
                            cnvCheckUpdate.setAlign(Alignment.CENTER);
                            cnvCheckUpdate.setShowTitle(Boolean.FALSE);
                                Img imgUpdated = new Img("systemeUpdated.png",16,16);
                            cnvCheckUpdate.setCanvas(imgUpdated);
                        
                            StaticTextItem stiTPCInformation = new StaticTextItem();
                            stiTPCInformation.setShowTitle(Boolean.FALSE);
                            stiTPCInformation.setWidth(300);
                            stiTPCInformation.setDefaultValue(APP_CONTACT_INFO);
                            
                            ButtonItem btnCloseAbout = new ButtonItem("","OK");
                            btnCloseAbout.setStartRow(Boolean.FALSE);
                            btnCloseAbout.setEndRow(Boolean.TRUE);
                            btnCloseAbout.setWidth(75);
                            btnCloseAbout.addClickHandler(new com.smartgwt.client.widgets.form.fields.events.ClickHandler() {
                                @Override
                                public void onClick(com.smartgwt.client.widgets.form.fields.events.ClickEvent event) {
                                    helpWindow.hide();
                                    helpWindow.sendToBack();
                                }
                            });
                        
                        formCloseAbout.setItems(cnvCheckUpdate,stiTPCInformation,btnCloseAbout);
                        
                        Label AppInformation = new Label(APP_CONTACT_INFO);
                        AppInformation.setStyleName("style_Text_Static_Small");
                        AppInformation.setMargin(5);
                        AppInformation.setWidth100();
                        AppInformation.setHeight("*");
                        
                    bottom.addMember(AppCopyright);
                    bottom.addMember(formCloseAbout);
                    
                    HTMLFlow separatorHTM = new HTMLFlow("<table width=100% height=2 cellpadding=0 cellspacing=0 border=0><tr><td style=\"height: 1px; background-color: "+window_about_form_bottom_bgcolor+"\"><img src=\"images/spacerGray.png\" width=1 height=1 border=0></td></tr><tr><td style=\"height: 1px; background-color: #FFFFFF\"><img src=\"images/spacer.png\" width=1 height=1 border=0></td></tr></table>");
                        
                winLayout.addMember(top);
                winLayout.addMember(separatorHTM);
                winLayout.addMember(bottom);
                
            helpWindow.addItem(winLayout);
            
            isHelpWindowBuilded = true;
        }
    }
    
    private com.smartgwt.client.widgets.Window getHelpWindow() {
        if (!isHelpWindowBuilded) { buildHelpWindow(); }
        return helpWindow;
    }
    
    private void logout() {
        systeme.startLoading();
        LoginDataSource loginDS = new LoginDataSource("LOGIN_FORM_DS");
        loginDS.logout(systeme);
    }
}